/*******************************************************************************
 * Copyright (c) 2011 EclipseDevX.org and others.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at:
 * 
 *     http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipsedevx.core.errors.ext;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.graphics.Image;
import org.eclipsedevx.core.DevXCorePlugin;

public class AnnotationTypesExtManager {

	private static AnnotationTypesExtManager SINGLETON;

	public static AnnotationTypesExtManager getModel() {
		if (SINGLETON == null) {
			SINGLETON = new AnnotationTypesExtManager();
		}

		return SINGLETON;
	}

	// org.eclipse.ui.editors.annotationTypes maps from markerType and severity to annotation ids

	// org.eclipse.ui.editors.markerAnnotationSpecification has all the details for annotation ids

	private Map<String, ImageData> imageDataForMarkerType = new HashMap<String, ImageData>();

	private AnnotationTypesExtManager() {
		parseExtensions();
	}

	public Image getImageForMarker(IMarker marker) {
		int severity = marker.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);

		// try {
		// String markerType = marker.getType();
		//      
		// ImageData data = imageDataForMarkerType.get(markerType + "." +
		// severity);
		//      
		// if (data != null) {
		// Image image = data.getImage();
		//        
		// if (image != null) {
		// return image;
		// }
		// }
		//      
		// data = imageDataForMarkerType.get(markerType);
		//      
		// if (data != null) {
		// Image image = data.getImage();
		//        
		// if (image != null) {
		// return image;
		// }
		// }
		// } catch (CoreException ce) {
		//      
		// }

		switch (severity) {
		case IMarker.SEVERITY_ERROR:
			return DevXCorePlugin.getImage("icons/misc/error_tsk.gif");
		case IMarker.SEVERITY_WARNING:
			return DevXCorePlugin.getImage("icons/misc/warn_tsk.gif");
		case IMarker.SEVERITY_INFO:
			return DevXCorePlugin.getImage("icons/misc/info_tsk.gif");
		}

		return null;
	}

	private void parseExtensions() {
		IConfigurationElement[] elements = Platform.getExtensionRegistry()
				.getConfigurationElementsFor("org.eclipse.ui.editors.annotationTypes");

		for (IConfigurationElement element : elements) {
			if ("type".equals(element.getName())) {

				String annotationId = element.getAttribute("name");

				String markerType = element.getAttribute("markerType");
				String markerSeverity = element.getAttribute("markerSeverity");

				if (annotationId != null) {
					ImageData data = findIconDataForAnnotation(annotationId);

					if (markerSeverity == null) {
						if (data != null) {
							imageDataForMarkerType.put(markerType + "."
									+ markerSeverity, data);
						}
					} else {
						if (data != null) {
							imageDataForMarkerType.put(markerType, data);
						}
					}
				}
			}
		}
	}

	private ImageData findIconDataForAnnotation(String annotationId) {
		IConfigurationElement[] elements = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(
						"org.eclipse.ui.editors.markerAnnotationSpecification");

		for (IConfigurationElement element : elements) {
			if ("specification".equals(element.getName())) {
				String annotationType = element.getAttribute("annotationType");
				String iconPath = element.getAttribute("icon");

				if (annotationId.equals(annotationType) && iconPath != null) {
					return new ImageData(element.getDeclaringExtension()
							.getContributor().getName(), iconPath);
				}
			}
		}

		return null;
	}

	private static class ImageData {
		public String pluginId;
		public String iconPath;

		public ImageData(String pluginId, String iconPath) {
			this.pluginId = pluginId;
			this.iconPath = iconPath;
		}

		@SuppressWarnings("unused")
		Image getImage() {
			return DevXCorePlugin.getImage("/" + pluginId + "/" + iconPath);
		}
	}

}
